/**
 * IndexOrderCursor.java
 * Copyright (C) 2011 Yuanyan<yuanyan.cao@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * For information on how to redistribute this software under
 * the terms of a license other than GNU General Public License
 * contact yuanyan.cao@gmail.com
 */
package edu.rabbit.kernel.table;


import edu.rabbit.Database;
import edu.rabbit.DbException;
import edu.rabbit.table.ICursor;
import edu.rabbit.table.ITransaction;

/**
 * @author Yuanyan<yanyan.cao@gmail.com>
 * 
 * 
 */
public class IndexOrderCursor extends TableDataCursor implements ICursor {

    protected String indexName;
    protected IBtreeIndexTable indexTable;

    /**
     * @param table
     * @param db
     * @throws DbException
     */
    public IndexOrderCursor(IBtreeDataTable table, Database db, String indexName) throws DbException {
        super(table, db);
        this.indexName = indexName != null ? indexName : table.getPrimaryKeyIndex();
        if (this.indexName != null) {
            this.indexTable = table.getIndexesTables().get(this.indexName);
        }
        first();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.internal.table.Cursor#first()
     */
    @Override
    public boolean first() throws DbException {
        return (Boolean) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                if (indexTable == null) {
                    return IndexOrderCursor.super.first();
                } else {
                    if (indexTable.first()) {
                        return firstRowNum(goTo(indexTable.getKeyRowId()));
                    }
                }
                return false;
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.internal.table.Cursor#next()
     */
    @Override
    public boolean next() throws DbException {
        return (Boolean) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                if (indexTable == null) {
                    return IndexOrderCursor.super.next();
                } else {
                    if (indexTable.next()) {
                        return nextRowNum(goTo(indexTable.getKeyRowId()));
                    }
                }
                return false;
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.internal.table.Cursor#eof()
     */
    @Override
    public boolean eof() throws DbException {
        return (Boolean) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                if (indexTable == null) {
                    return IndexOrderCursor.super.eof();
                } else {
                    return indexTable.eof();
                }
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.internal.table.Cursor#last()
     */
    @Override
    public boolean last() throws DbException {
        return (Boolean) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                if (indexTable == null) {
                    return IndexOrderCursor.super.last();
                } else {
                    if (indexTable.last()) {
                        return lastRowNum(goTo(indexTable.getKeyRowId()));
                    }
                }
                return false;
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.internal.table.Cursor#previous()
     */
    @Override
    public boolean previous() throws DbException {
        return (Boolean) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                if (indexTable == null) {
                    return IndexOrderCursor.super.previous();
                } else {
                    if (indexTable.previous()) {
                        return previousRowNum(goTo(indexTable.getKeyRowId()));
                    }
                }
                return false;
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.internal.table.TableDataCursor#delete()
     */
    @Override
    public void delete() throws DbException {
        if (indexTable != null) {
            goTo(indexTable.getKeyRowId());
        }
        super.delete();
        if (indexTable != null) {
            goTo(indexTable.getKeyRowId());
        }
    }

}
